package com.gz.common;
/**
 * 优惠劵Service
 * @author 86173
 *
 */

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.gz.common.model.Coupon;
import com.gz.common.model.User;
import com.gz.common.model.UserCoupon;

public class CouponService {
	
	private static CouponService couponService=null;
	
	public static CouponService getCouponService() {
		if(couponService==null) {
			couponService=new CouponService();
		}
		return couponService;
	}
	
	/**
	 * 查询兑换该优惠劵所需的积分
	 * author 绝爷
	 * @param id
	 * @return 该优惠劵兑换所需积分
	 */
	public Coupon findIntegral(Integer id) {
		List<Coupon> couponList = Coupon.dao.find("select * from tb_coupon where id=?",id);
		return couponList.size()>0?couponList.get(0):null;
	}
	
	/**
	 * 修改用户积分
	 * @param integral所需积分
	 * @param consumption消费积分
	 * @param amount提现积分
	 * @return
	 */
	public boolean changeCoupon(User user) {
		return user.update();
	}
	
	/**
	 * 新增用户优惠劵
	 */
	public boolean addUserCoupon(User user,Coupon coupon) {
		UserCoupon userCoupon=new UserCoupon();
        userCoupon.setUserId(user.getId());
        userCoupon.setCouponId(coupon.getId());
        userCoupon.setCreateTime(new Date());
        Calendar cd = Calendar.getInstance();
        cd.setTime(new Date());
        cd.add(Calendar.DATE, coupon.getPeriod());
        userCoupon.setExpiryDate(cd.getTime());
        return userCoupon.save();
	}
}
